<?php include 'checkUserLogin.php'?>

    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" type="text/css" href="../assets/css/main.css">
    <link href="../assets/css/bootstrap.min.css" rel="stylesheet">

    <script type="text/javascript" src="../assets/js/jquery-3.5.1.min.js"></script>
    <script type="text/javascript" src="../assets/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function () {//加载用户名
            var l_login = $("#l_login");
            // console.log("l_login:" + l_login)
            var l_logout = $("#l_logout");
            if (l_login.html() == '登录') {
                l_login.html('<?php echo $_SESSION["username"]  ?>')
                l_login.attr("href", "#");
                $(".showCartHeader").attr("href","../cart/cart.php?show_cart_id="+getCartId())
                l_logout.css("display", "block")
            }
        })
        /**
         * getUserId
         */
        function getUserId() {
            var uid = '';
            $.ajax({
                async: false,//取消异步执行，为同步执行。进而被其他方法调用
                url: "http://localhost:80/phalapi/public/index.php?s=App.User.Login",
                type: "POST",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    'username': '<?php echo $_SESSION["username"] ?>',
                    "password": '<?php echo $_SESSION["password"] ?>'
                }),
                dataType: "JSON",
                success: function (result) {
                    uid = result['data']['userid'];

                    // console.log("uid:"+uid)
                },
                error: function (msg) {
                    alert("错误信息：" + msg);
                }
            })
            return uid;
        }

        /**
         * getCartId
         */
        function getCartId() {
            var carid = ''
            $.ajax({
                async: false,//取消异步执行，为同步执行。进而被其他方法调用
                url: "http://localhost:80/phalapi/public/index.php?s=App.Cart.getCartInfoByUid",
                type: "GET",
                data: {
                    "user_id": getUserId()
                },
                dataType: "JSON",
                success: function (result) {
                    carid = result.data.info[0].id;
                    // console.log("data:" + username_data)

                },
                error: function (msg) {
                    alert("错误信息：" + msg);
                }
            })
            return carid;
        }
    </script>
    <!--header-->
    <nav class="navbar navbar-default" role="navigation">
        <button type="button" class="navbar-toggle" data-target="#menu" data-toggle="collapse" aria-expanded="false">
            <span class="sr-only">汉堡按钮</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <div class="navbar-header navbar-left">
            <a href="../index.php" class="navbar-brand">No.2商城</a>
        </div>
        <div id="menu" class="collapse navbar-collapse">
            <div class="navbar-left">
                <ul id="goodsType_nav_data" class="nav navbar-nav">
                </ul>
            </div>
            <div class="navbar-right">
                <ul class="nav navbar-nav">
                    <li><a id="l_login" href="../login/login.php">登录</a></li>
                    <li id="l_logout"><a href="../index.php?action=logout">注销</a></li>
                    <li><a target="_blank" href="../login/register.php">注册</a></li>
                    <li><a href="../order/orderList.php?isOLogin=lg">我的订单</a></li>
                    <li><a href="#">消息通知</a></li>
                    <li><a class="showCartHeader" href="../cart/cart.php?isCLogin=clg">购物车</a></li>
                    <li><a id="user_name" href=""></a></li>

                </ul>
            </div>
        </div>
    </nav>

    <script type="text/javascript">

        var l_login = $("#l_login");
        var l_logout = $("#l_logout");
        if (l_login.html() == "登录") {
            l_logout.css("display", "none")
        }

        /**
         * 获取商品类型列表
         */
        $(function () {
            $.ajax({
                url: "http://localhost:80/phalapi/public/index.php?s=App.GoodsType.getGoodsTypes",
                type: "GET",
                dataType: "JSON",
                success: function (result) {
                    var goodsType_data = result.data.info;
                    // console.log("data:" + username_data[0]['name'])
                    //<li><a href='goods/goodsList.php'></a></li>

                    var s = "";
                    for (var i = 0; i < goodsType_data.length; i++) {
                        var goodsType_name = goodsType_data[i]['name'];
                        s += "<li><a href='../goods/goodsList.php?actionGoodTypeName=" + goodsType_name +
                            "'> " + goodsType_name +
                            "</a></li>";
                    }
                    $("#goodsType_nav_data").html(s);
                },
                error: function (msg) {
                    alert("错误信息：" + msg);
                }
            })

        })
    </script>
<?php
